#include "clabelx.h"
#include "v2/xml/xml_device_object.h"

CLabelx::CLabelx(QObject *parent) :
    CKnxControl(parent)
{

}

void CLabelx::parseExp(QString exp)
{
    //表达式状态
    _decNo = 0;
    QStringList arrys = exp.split(";");
    if(arrys.size()!=0)
    {
        for(int i = 0;i<arrys.size();i++)
        {
            QStringList e = arrys.at(i).split("=");
            if (e.size()==2) {

                if (e.at(0) =="DEC") {
                    _decNo = e.at(1).toUInt();
                }
                else if (e.at(0) =="CALC") {
                    _calcArray = e.at(1).split(",");
                }
                else if (e.at(0) =="REPLACE") {
                    _replaceArray = e.at(1).split(",");
                }
                else if (e.at(0) =="MIN") {
                    _min = e.at(1).toFloat();
                }
                else if (e.at(0) =="MAX") {
                    _max = e.at(1).toFloat();
                }
            }
        }

    }
}

void CLabelx::setKnxObject(void *jOBJECT)
{
    //表达式状态
    _decNo = 0;

    Xml_DEVICE_OBJECT *object = (Xml_DEVICE_OBJECT *)jOBJECT;

    //反馈值
    QStringList arrys = object->_status.split(":");
    if(arrys.size() >= 3)
    {
        _feedbackAddress = arrys.at(0);
        _feedbackDataType =  (DATA_TYPE)arrys.at(1).toUInt();
        _feedbackValueDefine =  (DATA_DEFINE)arrys.at(2).toUInt();
    }

}

void CLabelx::readKnxAction()
{
    qDebug() <<"CLabelx readKnxAction";

    CKnxExecThread *eThread = new CKnxExecThread();
    eThread->ttgConnector = (CTTGIpsConnector *)cntDelegate;

    connect(eThread,SIGNAL(finished()),eThread,SLOT(deleteLater()));

    CExecStruct *s = new CExecStruct();
    s->address = _feedbackAddress;

    eThread->execList.append(s);

    eThread->start();
}

void CLabelx::execKnxAction(QString action,QString value)
{
    qDebug() <<"Label not execKnxAction";
}

QString CLabelx::valueCheck(QString action,QString value)
{
    if(_calcArray.size()>0)
    {
        float calcValue = value.toFloat();
        QString op,num;

        for(int i=0;i<_calcArray.size();i++)
        {
            if(op.isEmpty())
                op = _calcArray.at(i);

            if(num.isEmpty())
                num = _calcArray.at(i);

            if(!op.isEmpty() && !num.isEmpty())
            {
                if(op == "+")
                {
                    calcValue += num.toFloat();
                }
                else if(op == "-")
                {
                    calcValue -= num.toFloat();
                }
                else if(op == "*")
                {
                    calcValue = calcValue * num.toFloat();
                }
                else if(op == "/" && num.toFloat()!=0)
                {
                    calcValue = calcValue/num.toFloat();
                }

                op.clear();
                num.clear();
            }
        }

        if(calcValue<_min) calcValue = _min;
        if(calcValue>_max) calcValue = _max;

        QString ds = QString("%1").arg(calcValue, 0, ' ', _decNo);
        return ds;
    }

    if(_replaceArray.size()>0)
    {
        for(int i=0;i<_replaceArray.size();i++)
        {
            QStringList rlist = _replaceArray.at(i).split("|");
            if(rlist.size()==2)
            {
                if(rlist.at(0) == value)
                    return rlist.at(1);
            }
        }
    }

    return value;
}

QString CLabelx::actionFromStatus(QString address,QString value)
{
    return DEVICE_ACTION_READ;
}

void CLabelx::execDeviceAction(QString action,QString value,QString unit)
{
    qDebug() << "CLabelx will do nothing in execDeviceAction function";
}
